/**
 * $Id: SyncBussinessDataMrg.java,v 1.3 2006/11/06 07:02:05 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner.synchdata;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.SQLConst;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.util.db.DBConnectUtil;

/**
 * TODO
 * 
 * Revision History
 * 
 * 2006-7-25,star.Zhao,created it
 */
public class SyncBussinessDataMrg {

    private final static Log log = LogFactory
            .getLog(SyncBussinessDataMrg.class);
    private static SyncBussinessDataMrg mrg = new SyncBussinessDataMrg();

    private SyncBussinessDataMrg() {

    }

    public static SyncBussinessDataMrg getInstance() {
        if (null == mrg) {
            mrg = new SyncBussinessDataMrg();
        }
        return mrg;
    }

    public static void main(String[] args) {
        SyncBussinessDataMrg.getInstance().syncBussinessDataMth(0, "1860",
                "136");
    }

    /**
     * @return
     */
    public boolean syncBussinessDataMth(int syncType, String gatewayCode,
            String mobileStartWith) {
        boolean result = false;
        List list = null;
        Map sysConf = RunnerMgr.getInstance().getSystemConf();
        String destDBName = (String) sysConf.get(SYSConst.CONF_DB_MYSQL);
        String orgDBName = (String) sysConf.get(SYSConst.CONF_DB_GATEWAY);
        String saveForSyncData = "";
        String updateSQl = "";
        if (SYSConst.SYNC_BLACKMOBILE_TYPE == syncType) {
            list = getNeedSyncData(orgDBName, SQLConst.SYN_SEL_BLACKMOBILE,
                    gatewayCode, mobileStartWith);
            saveForSyncData = SQLConst.SYN_SAVE_BLACKMOBILE;
            updateSQl = SQLConst.SYN_UPD_BLACKMOBILE;
        } else if (SYSConst.SYNC_SENSEMOBILE_TYPE == syncType) {
            list = getNeedSyncData(orgDBName, SQLConst.SYN_SEL_SENESEMOBILE,
                    gatewayCode, mobileStartWith);
            saveForSyncData = SQLConst.SYN_SAVE_SENESEMOBILE;
            updateSQl = SQLConst.SYN_UPD_SENSEMOBILE;
        } else if (SYSConst.SYNC_BLACKLONGNUM_TYPE == syncType) {
            list = getNeedSyncData(orgDBName, SQLConst.SYN_SEL_BLACKLONGNUM,
                    gatewayCode, mobileStartWith);
            saveForSyncData = SQLConst.SYN_SAVE_BLACKLONGNUM;
            updateSQl = SQLConst.SYN_UPD_BLACKLONGNUM;
        } else {
            throw new IllegalArgumentException(
                    "Illegal argument in syncType field !!!");
        }

        // save
        Connection conn = null;
        PreparedStatement ps = null;
        if (null != list && list.size() > 0) {
            try {
                conn = DBConnectUtil.open(destDBName);
                ps = conn.prepareStatement(saveForSyncData);
                Iterator iterator = list.iterator();
                while (iterator.hasNext()) {
                    MoblieInfo info = (MoblieInfo) iterator.next();
                    ps.setString(1, info.getId());
                    ps.setString(2, info.getMobile());
                    ps.setString(3, info.getDetail());
                    ps.addBatch();
                }
                int res[] = ps.executeBatch();
                if (null != res && res.length == list.size()) {
                    result = true;
                } else {
                    result = false;
                }

            } catch (SQLException e) {
                log.error("SQLException in syncBussinessDataMth() !!!", e);
                result = false;
            } finally {
                if (null != ps) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        log.error(
                                "SQLException in close db prepareStatement!!!",
                                e);
                    }
                    ps = null;
                }

                if (null != conn) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        log.error("SQLException in close db connection!!!", e);
                    }
                    conn = null;
                }
            }
        }

        // 完备处理
        doDealed(updateSQl, orgDBName, list);

        // 释放空间
        list.clear();
        list = null;

        return result;
    }

    /**
     * @return
     */
    private List getNeedSyncData(String dbName, String sSQL,
            String gatewayCode, String mobileStartWith) {
        List result = new ArrayList();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        MoblieInfo moblieInfo = null;
        try {
            conn = DBConnectUtil.open(dbName);
            ps = conn.prepareStatement(sSQL);
            ps.setString(1, gatewayCode);
            rs = ps.executeQuery();

            while (rs.next()) {
                moblieInfo = new MoblieInfo();
                moblieInfo.setId(rs.getString(1));
                moblieInfo.setMobile(rs.getString(2));
                moblieInfo.setDetail(rs.getString(3));
                result.add(moblieInfo);
            }
        } catch (SQLException e) {
            log.error("SQLException in getNeedSyncData() !!!");
        } finally {
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    log.error("SQLException in close db prepareStatement!!!");
                }
                ps = null;
            }
            if (null != conn) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("SQLException in close db connection!!!");
                }
                conn = null;
            }
        }
        return result;
    }

    /**
     * 完备处理
     */
    private void doDealed(String sSQl, String dbName, List list) {
        if (null == list || list.size() == 0) {
            return;
        }

        Connection conn = null;
        PreparedStatement ps = null;
        MoblieInfo moblieInfo = null;
        try {
            conn = DBConnectUtil.open(dbName);
            ps = conn.prepareStatement(sSQl);
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                moblieInfo = (MoblieInfo) iterator.next();
                ps.setString(1, moblieInfo.getId());
                ps.addBatch();
            }

            ps.executeBatch();

        } catch (SQLException e) {
            log.error("SQLException in getNeedSyncData() !!!");
        } finally {
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    log.error("SQLException in close db prepareStatement!!!");
                }
                ps = null;
            }
            if (null != conn) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("SQLException in close db connection!!!");
                }
                conn = null;
            }
        }
    }
}

class MoblieInfo {
    private String id;
    private String mobile;
    private String detail;

    /**
     * @return Returns the detail.
     */
    public String getDetail() {
        return detail;
    }

    /**
     * @param detail The detail to set.
     */
    public void setDetail(String detail) {
        this.detail = detail;
    }

    /**
     * @return Returns the id.
     */
    public String getId() {
        return id;
    }

    /**
     * @param id The id to set.
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return Returns the mobile.
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @param mobile The mobile to set.
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}